package com.kizuki.algorithm.leetcode;

/**
 * 给定一个已排序的链表的头 head ， 删除原始链表中所有重复数字的节点，只留下不同的数字 。返回 已排序的链表 。
 *
 * https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/
 */
public class 删除排序链表中的重复元素II {

    class Solution {
        public ListNode deleteDuplicates(ListNode head) {
            if (head == null || head.next == null) {
                return head;
            }
            ListNode next = head.next;
            if (next.val == head.val) {
                while(next != null && next.val == head.val) {
                    next = next.next;
                }
                head =  deleteDuplicates(next);
            } else {
                head.next = deleteDuplicates(next);
            }
            return head;
        }
    }
}
